package com.fenghuajueli.module_user.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.fenghuajueli.libbasecoreui.ui.PrivacyWebActivity;
import com.fenghuajueli.libbasecoreui.constants.PrivacyConstantsUtils;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.databinding.DialogVipSubscribeBinding;

public class SubscribeDialog extends Dialog {


    public SubscribeDialog(@NonNull Context context) {
        super(context, com.fenghuajueli.libbasecoreui.R.style.StyleBaseDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogVipSubscribeBinding binding = DialogVipSubscribeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) binding.llContainer.getLayoutParams();
        int start = SizeUtils.dp2px(32);
        layoutParams.setMarginStart(start);
        layoutParams.setMarginEnd(start);
        binding.llContainer.setLayoutParams(layoutParams);


        String text = "\t\t\t\t购买订阅会员后，将根据您购买的订阅会员类型续订（在本周期到期前24小时，为您自动续费并延长对应周期时长）。您可以在下一个扣费日期前取消订阅。\n\n\t\t\t\t取消订阅：在“支付宝”-“我的”-“右上角设置”-“支付设置”-“免密支付/自动扣款”-找到对应服务“关闭服务”即可取消订阅。\n\n\t\t\t\t在购买订阅会员前，请您先阅读并同意《订阅协议》，如有其他问题可随时联系我们客服，感谢您的使用。";
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(getContext(),R.color.vipCenterSubTextColor));
                //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线,其实默认也是true，如果要下划线的话可以不设置
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(@NonNull View widget) {
                PrivacyWebActivity.start(getContext(), PrivacyConstantsUtils.ProtocolType.SUBSCRIBE_PROTOCOL);
            }
        };
        builder.setSpan(clickableSpan, text.length() - 30, text.length() - 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        binding.tvContent.setText(builder);
        binding.tvContent.setMovementMethod(LinkMovementMethod.getInstance());

        binding.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

}
